package cn.life.common.controller;

import cn.life.common.support.check.UserInterceptorHandler;
import cn.life.common.utils.FileUploadUtil;
import cn.life.common.utils.PropertySourceConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;

/**
 * @Description:
 *
 * @Author 李佳明
 * @Date 2018-08-17 14:40
 */
@RestController
@RequestMapping("image")
@PropertySource({"classpath:config.properties"})
public class FileController {

    @Autowired
    private Environment env;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String fileName = new Date().getTime() + "-" + file.getOriginalFilename();
        String imageUrl = request.getServletContext().getRealPath("/") + "images";
        //String imageUrl=System.getProperty("catalina.home")  + "/webapps/djkWeb/djkImg/";
        File targetFile=new File(imageUrl,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        try{
            file.transferTo(targetFile);
        }catch(Exception e){
            e.printStackTrace();
        }
        //String str = FileUploadUtil.uploadFile(file.getBytes(), imageUrl, fileName);
        //str得到的是一个七牛返回的外链，然后将str存到自己的服务器
        return fileName;
    }

    @RequestMapping(value = "/up",method = RequestMethod.POST)
    public String up(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) throws Exception {
        String prefixUrl = env.getProperty("imgUrl");

        response.setHeader("Access-Control-Allow-Origin","*");
        String fileName = new Date().getTime() + "-" + file.getOriginalFilename();
        String imageUrl = request.getServletContext().getRealPath("/") + "images";
        //String imageUrl=System.getProperty("catalina.home")  + "/webapps/djkWeb/djkImg/";
        File targetFile=new File(imageUrl,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        try{
            file.transferTo(targetFile);
        }catch(Exception e){
            e.printStackTrace();
        }
        //String str = FileUploadUtil.uploadFile(file.getBytes(), imageUrl, fileName);
        //str得到的是一个七牛返回的外链，然后将str存到自己的服务器
        return prefixUrl + fileName;
    }
}
